package com.building.hcrm.ui.customer;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.building.hcrm.R;
import com.building.hcrm.R.id;
import com.building.hcrm.R.string;
import com.building.hcrm.base.BaseActivity;
import com.building.hcrm.common.ComConst;
import com.widget.time.JudgeDate;
import com.widget.time.ScreenInfo;
import com.widget.time.WheelMain;

/**
 * 客户跟进
 * 
 * @author hby
 * 
 */
public class CustomerFollow extends BaseActivity {

	private WheelMain wheelMain;
	private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

	private EditText etFollowTime;
	private ImageView ivFollowDate;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.partial_customer_follow);
		initControls();
		initDats();
		setOnAllListeners();
	}

	private void initControls() {
		etFollowTime = (EditText) findViewById(id.et_follow_time);
		ivFollowDate = (ImageView) findViewById(id.iv_datepicker_follow);
		imgbtnHome = (ImageButton) findViewById(id.imgbtn_home);
		imgbtnHome.setVisibility(View.VISIBLE);
	}

	private void initDats() {
		String time = getIntent().getStringExtra(ComConst.CUSTOMER_FOLLOW_TIME)
				.toString();
		if (!("").equals(time)) {
			etFollowTime.setText(time);
		}
		setTitle(getResources().getString(string.customer_title_follow));
	}

	private void setOnAllListeners() {
		etFollowTime.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// 设置点击EditText不弹出系统的虚拟键盘
				// backup the input type
				int inType = etFollowTime.getInputType();
				// disable soft input
				etFollowTime.setInputType(InputType.TYPE_NULL);
				// call native handler
				etFollowTime.onTouchEvent(event);
				// restore input type
				etFollowTime.setInputType(inType); // restore input type
				etFollowTime.setSelection(etFollowTime.getText().length());
				return true;
			}
		});

		imgbtnHome.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// getDatas();
				Intent intent = new Intent();
				intent.putExtra(ComConst.CUSTOMER_FOLLOW_TIME, etFollowTime
						.getText().toString());
				setResult(ComConst.CUSTOMER_FOLLOW, intent);
				imgbtnHome.setVisibility(View.INVISIBLE);
				finish();
				slideReturnActivity();
			}
		});

		ivFollowDate.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				LayoutInflater inflater = LayoutInflater
						.from(CustomerFollow.this);
				final View timepickerview = inflater.inflate(
						R.layout.item_time_picker, null);
				ScreenInfo screenInfo = new ScreenInfo(CustomerFollow.this);
				wheelMain = new WheelMain(timepickerview);
				wheelMain.screenheight = screenInfo.getHeight();
				String time = etFollowTime.getText().toString();
				Calendar calendar = Calendar.getInstance();
				// 显示时间
				if (JudgeDate.isDate(time, "yyyy-MM-dd")) {
					try {
						calendar.setTime(dateFormat.parse(time));
					} catch (ParseException e) {
						e.printStackTrace();
					}
				}
				int year = calendar.get(Calendar.YEAR);
				int month = calendar.get(Calendar.MONTH);
				int day = calendar.get(Calendar.DAY_OF_MONTH);
				wheelMain.initDateTimePicker(year, month, day);
				new AlertDialog.Builder(CustomerFollow.this)
						.setTitle("时间选择")
						.setView(timepickerview)
						.setPositiveButton("确定",
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										etFollowTime.setText(wheelMain
												.getTime());
									}
								})
						.setNegativeButton("取消",
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											int which) {
									}
								}).show();
			}
		});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			imgbtnHome.performClick();
		}
		return false;
	}

}
